package com.ruoyi.project.WebMoudle.carousel.controller;

import java.io.IOException;
import java.util.List;

import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.RuoYiConfig;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.WebMoudle.carousel.domain.Carousel;
import com.ruoyi.project.WebMoudle.carousel.service.ICarouselService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 轮播管理Controller
 * 
 * @author ruoyi
 * @date 2020-06-30
 */
@Controller
@RequestMapping("/carousel/carousel")
public class CarouselController extends BaseController
{
    private String prefix = "carousel/carousel";

    @Autowired
    private ICarouselService carouselService;

    @RequiresPermissions("carousel:carousel:view")
    @GetMapping()
    public String carousel()
    {
        return prefix + "/carousel";
    }

    @RequiresPermissions("carousel:carousel:view")
    @GetMapping("/okList")
    public String okList()
    {
        return prefix + "/okList";
    }

    @RequiresPermissions("carousel:carousel:view")
    @GetMapping("/noList")
    public String noList()
    {
        return prefix + "/noList";
    }


    @GetMapping("/selectShop")
    public String selectShop()
    {
        return prefix + "/selectShop";
    }

    /**
     * 查询轮播管理列表
     */
    @RequiresPermissions("carousel:carousel:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Carousel carousel)
    {
        startPage();
        List<Carousel> list = carouselService.selectCarouselList(carousel);
        return getDataTable(list);
    }

    /**
     * 导出轮播管理列表
     */
    @RequiresPermissions("carousel:carousel:export")
    @Log(title = "轮播管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Carousel carousel)
    {
        List<Carousel> list = carouselService.selectCarouselList(carousel);
        ExcelUtil<Carousel> util = new ExcelUtil<Carousel>(Carousel.class);
        return util.exportExcel(list, "carousel");
    }

    /**
     * 新增轮播管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存轮播管理
     */
    @RequiresPermissions("carousel:carousel:add")
    @Log(title = "轮播管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestParam("imgFile") MultipartFile imgFile, Carousel carousel)
    {
        return toAjax(carouselService.insertCarousel(imgFile,carousel));
    }

    /**
     * 修改轮播管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Carousel carousel = carouselService.selectCarouselById(id);
        mmap.put("carousel", carousel);
        return prefix + "/edit";
    }

    /**
     * 修改保存轮播管理
     */
    @RequiresPermissions("carousel:carousel:edit")
    @Log(title = "轮播管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestParam("imgFile") MultipartFile imgFile,Carousel carousel)
    {
        try {
            //上传图片
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String imgName = FileUploadUtils.upload(filePath, imgFile);
            carousel.setImgUrl(imgName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return toAjax(carouselService.updateCarousel(carousel));
    }

    /**
     * 删除轮播管理
     */
    @RequiresPermissions("carousel:carousel:remove")
    @Log(title = "轮播管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(carouselService.deleteCarouselByIds(ids));
    }

    @RequiresPermissions("carousel:carousel:edit")
    @PostMapping("/checkStatusUnique")
    @ResponseBody
    public String checkStatusUnique(Carousel carousel)
    {
        return carouselService.checkStatusUnique(carousel);
    }
}
